<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'
export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '220px'
        },
        pieData: {
            type: Object,
            // required: true
        }
    },
    data() {
        return {
            chart: null,
            normalData: 0,
            forewarningData: 0,
            offlineData: 0
        }
    },
    watch: {
        pieData: {
            deep: true,
            handler(val) {
                this.setOptions(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },

    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            this.setOptions(this.pieData)
            // this.setOptions()
        },
        setOptions(numData) {
            let _this = this;
            this.chart.setOption({
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                series: [
                    {
                        name: '设备状况',
                        type: 'pie',
                        avoidLabelOverlap: true,
                        label: {
                            show: false,
                        },
                        data: [
                            {
                                name: '正常设备',
                                value: numData.normalData
                            },
                            {
                                name: '预警设备',
                                value: numData.forewarningData
                            }, {
                                name: '离线设备',
                                value: numData.offlineData
                            }

                        ],
                    }
                ]
            })
        }
    }
}
</script>
